package com.spark.gaea.utils;

import java.security.MessageDigest;

public final class MD5 {
	public static String md5for32(final byte[] byteArray) {
		String md5Str = null;
		try {
			final StringBuffer buf = new StringBuffer();
			final MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(byteArray);
			final byte[] b = md.digest();
			for (int offset = 0; offset < b.length; ++offset) {
				int i = b[offset];
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			md5Str = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return md5Str;
	}

	public static String md5for16(final byte[] byteArray) {
		return md5for32(byteArray).substring(8, 24);
	}

	public static String md5for32(final String plainText) {
		return md5for32(plainText.getBytes());
	}

	public static String md5for16(final String plainText) {
		return md5for32(plainText).substring(8, 24);
	}
}
